'use strict';

const vinylFs = require('vinyl-fs');
const gulpJade = require('gulp-jade');
const jade = require('jade');
const wrap = require('gulp-wrap-umd');
const util = require('util');
const gutil = require('gulp-util');

module.exports = function (src, dest, resolve, reject) {
	gutil.log('开始执行jade2js任务', src);
	vinylFs.src(src)
	.pipe(gulpJade({
			jade : jade,
			client : true
		}))
	.pipe(wrap({
			namespace : 'Htemplates',
			deps : [{
					name : 'jade',
					globalName : 'jade',
					cjsName : 'jade/lib/runtime',
					amdName : 'jade/lib/runtime'
				}
			]
		})).on('error', function (err) {
		if (util.isFunction(reject)) {
			reject(err);
		}
	})
	.on('end', function () {
		if (util.isFunction(resolve)) {
			setTimeout(function() {
				resolve();
			}, 100);
		}
	})
	.pipe(vinylFs.dest(dest));
};
